<template>
    <div class="history">
        <div class="title">
            <span class="text">搜索历史</span>
            <div class="btns">
                <div class="txtBtn" v-if="showDel">
                    <span @click="dels">全部删除</span>
                    <span @click="showDel = false">完成</span>
                </div>
                <div class="iconBtn">
                    <van-icon name="delete-o" v-if="!showDel" @click="showDel = true" />
                </div>
            </div>
        </div>
        <div class="history-list">
            <div class="item" v-for="(item, index) in listData" :key="index" @click="showSearch(item)">
                <span>{{ item }}</span>
                <van-icon name="clear" @click="del(index)" v-if="showDel" />
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'history',
    props: {
        keyword: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            listData: [
                "前端",
                "后端",
                "移动端",
                "小程序",
                "uniapp",
                "vue",
                "react"
            ],
            showDel: false
        }
    },
    watch: {
        keyword(nv, ov) {
            if (nv) {
                this.listData.unshift(nv);
            }
        }
    },
    methods: {
        dels() {
            this.listData = [];
        },
        del(i) {
            this.listData.splice(i, 1);
        },
        showSearch(item) {
            if (!this.showDel) {
                this.$emit('onSearch', 3, item);
                const i = this.listData.findIndex(ele => ele == item);
                if (i > -1) {
                    console.log(11);
                    this.listData.splice(i, 1);
                    this.listData.unshift(item);
                }
            }
        }
    }
}
</script>
<style lang="less" scoped>
.history {
    padding: 10px;

    .title {
        display: flex;
        justify-content: space-between;
        height: 25px;

        .text {
            font-size: 12px;
        }

        .btns {
            display: flex;
            font-size: 12px;

            .txtBtn {
                color: #999;
                line-height: 20px;

                span {
                    margin-left: 10px;
                }
            }

            .iconBtn {
                font-size: 16px;
            }
        }
    }

    .history-list {
        display: flex;
        flex-wrap: wrap;

        font-size: 14px;

        .item:nth-child(odd)::before {
            content: '';
            border-right: 1px solid #000;
            height: 10px;
            float: right;
            position: relative;
            top: 5px;
        }

        .item {
            width: 50%;
            margin: 5px 0;
            padding: 0 5px;

            .van-icon {
                float: right;
                position: relative;
                top: 5px;
                margin-right: 10px;
            }
        }
    }
}
</style>